package zhieasy.cn.xpu_helper.controller.forum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.forum.ForumCommentRely;
import zhieasy.cn.model.forum.ForumCommentRoot;
import zhieasy.cn.service.forum.ForumCommentRelyService;
import zhieasy.cn.service.forum.ForumCommentRootService;

import java.util.List;

/**
 * @author DaHang
 * @date 2020/8/30 16:53
 */
@RestController
@RequestMapping("/forum")
@Api(tags = "论坛模块，评论相关")
public class ForumCommentController {

    @Autowired
    ForumCommentRootService forumCommentRootService;

    @Autowired
    ForumCommentRelyService forumCommentRelyService;

    @RequestMapping(value = "/common",method = RequestMethod.POST)
    @ApiOperation(value = "给帖子增加根评论",notes = "就是一级评论，评论的评论叫做二级评论")
    public ResultVO addRootCommon(@ApiParam("论坛评论类")@RequestBody ForumCommentRoot forumCommentRoot){
        forumCommentRoot.setCreateTime(System.currentTimeMillis());
        forumCommentRoot.setValid(1);
        boolean idSave = forumCommentRootService.save(forumCommentRoot);
        if (idSave) {
            return ResultUtils.success();
        }
        return ResultUtils.error("评论添加失败");
    }


    @RequestMapping(value = "/common/{rootCommonId}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除根评论",notes = "删除根评论，也就是一级评论")
    public ResultVO deleteRootCommon(@ApiParam("根评论的id")@PathVariable("rootCommonId")String rootCommonId){
        boolean b = forumCommentRootService.removeById(rootCommonId);
        if (b) {
            return ResultUtils.success();
        }
        return ResultUtils.error();
    }

    @RequestMapping(value = "/common/{forumId}",method = RequestMethod.GET)
    @ApiOperation(value = "获取评论",notes = "获取某个帖子的评论")
    public ResultVO getRootCommonByForumId(@ApiParam("要获取评论的帖子的id")@PathVariable("forumId")int forumId,
                                           @ApiParam("页数")@RequestParam("pageIndex")int pageIndex,
                                           @ApiParam("每页要多少数据")@RequestParam("pageSize")int pageSize){

        List<ForumCommentRoot> forumCommentRootList = forumCommentRootService.getCommonRootByForumId(forumId, pageIndex, pageSize);
        return ResultUtils.success();
    }


    @RequestMapping(value = "/common/reply",method = RequestMethod.POST)
    @ApiOperation(value = "给帖子增加根评论",notes = "就是一级评论，评论的评论叫做二级评论")
    public ResultVO addCommonToRoot(@ApiParam("论坛评论类")@RequestBody ForumCommentRely forumCommentRely){
        forumCommentRely.setCreateTime(System.currentTimeMillis());
        forumCommentRely.setValid(1);
        boolean idSave = forumCommentRelyService.save(forumCommentRely);
        if (idSave) {
            return ResultUtils.success();
        }
        return ResultUtils.error("评论添加失败");
    }



}
